﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.UserControl" %>
<%@ Import Namespace="Portal.Core.Utils" %>
<%@ Import Namespace="Portal.Data" %>
<%@ Import Namespace="Portal.Data.Entity" %>
<%@ Import Namespace="Portal.Data.Security" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SecurityProvider.AllowCall())
            return;

        var user = PortalContext.CurrentUser;

        var action = Request["act"];
        dynamic jdata = new DynamicJsonObject();
        int bookId;
        Book b;
        switch (action)
        {
            case "switchHot":
                if (user == null)
                    return;
                int.TryParse(Request["bid"], out bookId);
                b = Book.FindByPrimaryKey(bookId);
                if (b != null)
                {
                    b.IsHot = !b.IsHot;   //switched
                    b.UpdateOnly();
                    Response.Write(b.IsHot);
                    Response.End();
                }
                break;
            case "switchFinish":
                if (user == null)
                    return;
                int.TryParse(Request["bid"], out bookId);
                b = Book.FindByPrimaryKey(bookId);
                if (b != null)
                {
                    b.Finish = !b.Finish;   //switched
                    b.UpdateOnly();
                    Response.Write(b.FinishDisplay);
                    Response.End();
                }
                break;
            case "switchStatus":
                if (user == null)
                    return;
                int.TryParse(Request["bid"], out bookId);
                b = Book.FindByPrimaryKey(bookId);
                if (b != null)
                {
                    b.Status = !b.Status;   //switched
                    b.UpdateOnly();
                    Response.Write(b.Status);
                    Response.End();
                }
                break;
            case "switchUniquee":
                if (user == null)
                    return;
                int.TryParse(Request["bid"], out bookId);
                b = Book.FindByPrimaryKey(bookId);
                if (b != null)
                {
                    b.IsUnique = !b.IsUnique;   //switched
                    b.UpdateOnly();
                    jdata.success = 1;
                    Response.Write(b.IsUnique);
                    Response.End();
                }
                break;
            case "load":
                var keyword = Request["keyword"] ?? "";
                var ids = new ArrayList();
                var titles = new ArrayList();
                if (keyword.Length > 0)
                {
                    var list = Book.QuickSearch(keyword, 0);
                    foreach (Book item in list)
                    {
                        ids.Add(item.ID);
                        titles.Add(string.Format(PortalContext.BookByAccountDisplayFormat, item.ID, item.FullName, item.TotalChap, item.TotalVisit, item.Status));
                    }
                }
                jdata.id = ids;
                jdata.label = titles;
                break;
            case "getAutoNextChap":
                int.TryParse(Request["bid"], out bookId);
                if (bookId > 0)
                {
                    var last = Portal.Data.Entity.Content.FindLatest(bookId);
                    jdata.nextchapter = last != null ? last.Chapter + 1 : 0;
                }
                break;
            case "quicksearch":
                var key = Request["key"] ?? "";
                if (key.Length > 0)
                {
                    key = key.ToLower().Trim();
                    var all = Book.QuickSearch(key, 0);
                    var books = all.Take(10).ToList();
                    jdata.url = books.Select(x => x.Url).ToArray();
                    jdata.label = books.Select(x => HightLight(key, x.FullName)).ToArray();
                    jdata.view = books.Select(x => x.TotalVisit).ToArray();
                    jdata.lastUpdateDisplay = books.Select(x => "Chương <b>" + x.TotalChap + "</b> từ " + Portal.Core.Common.FriendlyDateDisplay(x.LastChapDate)).ToArray();
                    jdata.rate = books.Select(x => x.RateAverage).ToArray();
                    jdata.finishDisplay = books.Select(x => x.FinishDisplay).ToArray();
                    jdata.more = books.Count < all.Count;
                }
                break;
        }

        Response.Write(jdata.ToString());
        Response.End();
    }
    string HightLight(string key, string text)
    {
        var lower = Process.RejectMarksOnly(text).ToLower();
        var idx = lower.IndexOf(key);
        if (idx >= 0)
        {
            return string.Format("{0}<span class='highlight'>{1}</span>{2}", text.Substring(0, idx), text.Substring(idx, key.Length), text.Substring(idx + key.Length));
        }
        return text;
    }
</script>